<%@ taglib prefix="C" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: 86152
  Date: 2022/4/27
  Time: 8:44
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%--    <jsp:forward page="/${requestScope.page.url}"></jsp:forward>--%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>书城首页</title>

    <%--    样式--%>
    <%@include file="/pages/common/plug_.jsp" %>
    <%--    样式--%>
    <script type="text/javascript">
        $(function () {
            $("button.btn_add").click(function () { // 加入购物车
                var id = $(this).attr("itemid");
                if (${empty sessionScope.cart.items}) {
                    location.href = "${pageScope.Path}/cartServlet?action=addItem&id=" + id;
                } else {
                    $.getJSON("${pageScope.Path}/cartServlet", "action=AjaxaddItem&id=" + id, function (data) {
                        console.log(data);
                        $("#item_name").text(data.lastItem);
                        $("#item_totalCount").text("您的购物车中有" + data.totalCart + "件商品")
                    })
                }
            })
        })
    </script>
</head>
<body>
<!-- header -->
<%@include file="/pages/common/success_head.jsp" %>
<!-- header -->

<div id="header">
    <span class="wel_word">商城管理系统简略版</span>
    <div>
        <C:if test="${empty sessionScope.user}">
            <a href="${pageScope.Path}/pages/user/Login.jsp">登录</a> |
            <a href="${pageScope.Path}/pages/user/Regist.jsp">注册</a>
        </C:if>
        <C:if test="${not empty sessionScope.user}">
            <span>欢迎 <span style="color: red;font-size: 20px;">${sessionScope.user.username}</span></span>
            <a href="${pageScope.Path}/userServlet?action=logout">注销</a>
        </C:if>
        &nbsp;&nbsp;
        <a href="${pageScope.Path}/pages/cart/cart.jsp">购物车</a>
        <a href="${pageScope.Path}/pages/manager/manager.jsp">后台管理</a>
    </div>
</div>

<div id="main">
    <div id="book">
        <div class="book_cond">
            <form action="${pageScope.Path}/clients/clientCommodity" method="get">
                <input type="hidden" name="action" value="pageByPrice">
                价格：<input id="min" type="text" name="min" value="${param.min}"> 元 -
                <input id="max" type="text" name="max" value="${param.max}"> 元
                <input type="submit" value="查询"/>
            </form>
        </div>

        <C:if test="${empty sessionScope.cart.items}">
            <div style="text-align: center">
                <span style="color: red">当前购物车为空</span>
            </div>
        </C:if>
        <C:if test="${not empty sessionScope.cart.items}">
            <div style="text-align: center">
                <span id="item_totalCount">您的购物车中有${sessionScope.cart.totalCount}件商品</span>
                <div>
                    您刚刚将<span style="color: red" id="item_name">${sessionScope.lastItem}</span>加入到了购物车中
                </div>
            </div>
        </C:if>

        <%--    list--%>
        <C:forEach items="${requestScope.page.commodities}" var="commodity">
            <div class="b_list">
                <div class="img_div">
                    <img class="book_img" alt="" src="${pageScope.Path}/${commodity.imgpath}"/>
                </div>
                <div class="book_info">
                    <div class="book_name">
                        <span class="sp1">商品名:</span>
                        <span class="sp2">${commodity.name}</span>
                    </div>
                    <div class="book_author">
                        <span class="sp1">作者:</span>
                        <span class="sp2">${commodity.author}</span>
                    </div>
                    <div class="book_price">
                        <span class="sp1">价格:</span>
                        <span class="sp2">￥${commodity.price}</span>
                    </div>
                    <div class="book_sales">
                        <span class="sp1">销量:</span>
                        <span class="sp2">${commodity.sales}</span>
                    </div>
                    <div class="book_amount">
                        <span class="sp1">库存:</span>
                        <span class="sp2">${commodity.stock}</span>
                    </div>
                    <div class="book_add">
                        <button itemid="${commodity.id}" class="btn_add">加入购物车</button>
                    </div>
                </div>
            </div>
        </C:forEach>
        <%--    list--%>
    </div>

    <%--    paging--%>
    <%@include file="/pages/common/paging.jsp" %>
    <%--    paging--%>

</div>

<!--footer-->
<%@include file="/pages/common/common_footer.jsp" %>
<!--footer-->

</body>
</html>